package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.dao.impl.UserDaoImpl;
import com.qf.xiaomi.pojo.TbAddress;
import com.qf.xiaomi.pojo.User;
import com.qf.xiaomi.service.impl.UserServiceImpl;
import com.qf.xiaomi.utils.EmailUtils;
import com.qf.xiaomi.utils.MD5Utils;
import com.qf.xiaomi.utils.StringCheck;
import com.qf.xiaomi.utils.StringFinalUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Created by 马英超 on 2022/8/15 17:36
 * @author： 马英超
 * @version：v1.0
 * @description:
 */

public class UserServlet {


    /**
     * 用户注册
     *
     *
     *
     * */
    protected String register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserServiceImpl userService=new UserServiceImpl();
        //获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String gender = req.getParameter("gender");
        //字符转换设置
        gender=new String(gender.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);


        User user=new User();
        user.setUsername(username);
        user.setPassword(MD5Utils.md5(password));
        user.setEmail(email);
        user.setGender(gender);
        //校验是否合法
        //通过工具类依次传入参数
//        final boolean aNull = StringCheck.isNull(username, password, email, gender);//是否为空校验,true为空
//        final boolean checkRegist = StringCheck.checkRegister(username, password, email);//判断姓名、密码、右键是否合法4*/
        final boolean checkResult = StringCheck.checkRegister(user);
        //如果校验通过则调用service执行注册
        if (user!=null && checkResult== true){

            final boolean registResutl = userService.registerUser(user);
            if(registResutl==true){//注册成功


                return  "redirect:/registerSuccess.jsp";


            }
            return  "redirect:/register.jsp?error='注册信息不合法！+1'";
        }else{//如果注册不通过，则返回注册页面，并将注册失败信息一并返回

            return  "redirect:/register.jsp?error='注册信息不合法！+2'";
        }

    }

    /**
     * 登录验证
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession httpSession=req.getSession();

        final String username = req.getParameter("username");
        final String password = req.getParameter("password");
        final String code = req.getParameter("code");

        //校验密码、用户名是否合规


        //校验码检查
        final String code1 = (String)httpSession.getAttribute("code");
       // httpSession.removeAttribute("code");//销毁session
        System.out.println("code1\t"+code1);
        System.out.println("code\t"+code);
        if(code.isEmpty()){
            return "redirect:/login.jsp?error='验证码不能为空'";
        }

        if (!code1.equalsIgnoreCase(code)){
            //验证码不正确
            return "redirect:/error/error.jsp?error='验证码不正确，请重新输入'";
        }

        if (username!=null){
            //调用Service
            UserServiceImpl userService=new UserServiceImpl();
            final User user = userService.userLogin(username, password);
            if (user!=null){//登录成功
            //
                httpSession.setAttribute("user",user);
                //设置session有效时间
               // httpSession.setMaxInactiveInterval(259200);//3天有效

                System.out.println(httpSession.getAttribute("user"));
                return "redirect:/index.jsp";
            }else{
                return "redirect:/error/error.jsp";
            }
        }
        return "redirect:/error/error.jsp";

    }

    /**
     * 注销登录
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
  /*  protected String logOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //退出登录需要销毁HttpSession
        req.getSession().invalidate();
        //重定向到登录页面

        return "redirect:/login.jsp";
    }*/

    protected String getAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 地址管理：
         *      需要信息：aname、aphone、adetail、astate
         *      地址表现有信息：id、detail、name、phone、uid、level
         */
        //
        final HttpSession session = req.getSession();
        //获取用户登录信息，从session域中
        final User user =(User) session.getAttribute("user");
        //调用UserService
        UserServiceImpl userService=new UserServiceImpl();
        final List<TbAddress> list = userService.queryAddress(user.getId());
        if (list.size()>0){
            session.setAttribute("list",list);
            return "redirect:/self_info.jsp" ;
        }else{
            return  "redirect:/error/error.jsp";
        }

    }


}


